<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from '@/components/Charts/mixins/resize'

export default {
  name: 'BarLarge',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      categoryData: [],
      valueData: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.generateData(2000)
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '过去一小时每辆车文件数量',
          left: 10
        },
        toolbox: {
          feature: {
            dataZoom: {
              yAxisIndex: false
            },
            saveAsImage: {
              pixelRatio: 2
            }
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },

        grid: {
          left: 0,
          right: 10,
          bottom: 50,
          top: 35,
          containLabel: true
        },
        dataZoom: [
          {
            type: 'inside'
          },
          {
            type: 'slider'
          }
        ],
        xAxis: {
          data: this.categoryData,
          silent: false,
          splitLine: {
            show: false
          },
          splitArea: {
            show: false
          }
        },
        yAxis: {
          splitArea: {
            show: false
          }
        },
        series: [
          {
            type: 'bar',
            data: this.valueData,
            // Set `large` for large data amount
            large: true
          }
        ]
      })
    },
    generateData(count) {
      let baseValue = Math.random() * 1000
      let time = +new Date(2011, 0, 1)
      let smallBaseValue

      function next(idx) {
        smallBaseValue =
          idx % 30 === 0
            ? Math.random() * 700
            : smallBaseValue + Math.random() * 500 - 250
        baseValue += Math.random() * 20 - 10
        return Math.max(0, Math.round(baseValue + smallBaseValue) + 3000)
      }

      const categoryData = []
      const valueData = []
      for (let i = 0; i < count; i++) {
        categoryData.push(
          echarts.format.formatTime('yyyy-MM-dd\nhh:mm:ss', time, false)
        )
        valueData.push(next(i).toFixed(2))
        time += 1000
      }
      this.categoryData = categoryData
      this.valueData = valueData
    }
  }
}

</script>

<style scoped>

</style>
